#!/usr/bin/python

import pygame
import sys
import Block
from pygame.locals import *

class Room:

	def __init__( self, map_x_coord, map_y_coord, x_dim, y_dim, x_block_dim, y_block_dim, path, name ) :
		self.x_dim = x_dim
		self.y_dim = y_dim
		self.x_block_dim = x_block_dim
		self.y_block_dim = y_block_dim
		self.max_x_index = x_dim/x_block_dim 
		self.max_y_index = y_dim/y_block_dim 
		self.map_x_coord = map_x_coord
		self.map_y_coord = map_y_coord
		self.path = path
		self.name = name
		self.csv_path = path + '/' + name + '/' + name + '.csv'
		self.image_path = path + '/' + name + '/' + name + '.jpg'	
		self.image = pygame.image.load(self.image_path).convert()
		self.is_active = False
		
		# create room array
		self.room = []  # array of block objects
		for i in xrange(self.max_x_index):
			self.room.append([])
			for j in xrange(self.max_y_index):
				self.room[i].append(Block.Block(i,j))

		self.syncCSV( self.csv_path )

	def getScreenCoords( self ):
		return (self.x_dim, self.y_dim)

	def getRoom( self ):
		return self.room

	def getImage( self ):
		return self.image

	def makeActive( self, active ):
		self.is_active = active

	def getActive( self ):
		return self.is_active

	def getCharBlock( self ):
		for i in xrange(self.max_x_index):
			for j in xrange(self.max_y_index):
				if self.room[i][j].containsChar():
					return self.room[i][j]
		return None

	# private method for setting initial state of blocks
	def syncCSV( self, filepath ):
		# build 2d array from csv data
		csv = open(filepath, 'r')
		csv_array = []
		i = 0
		for line in csv:
			temp = line.replace('\n','')
			csv_array.append([])
			for subline in temp.split(','):
				csv_array[i].append(subline)
			i += 1

		#sync 2d csv array with block array
		for i in xrange(self.max_x_index):
			temp = self.room[i]
			for j in xrange(self.max_y_index):
				if csv_array[i][j].find('x') != -1:
					self.room[j][i].setWall(True)
				if csv_array[i][j].find('s') != -1:
					self.room[j][i].setChar(True)
		
				
	def addTransition( self, source_x, source_y, room, dest_x, dest_y ):
		self.room[source_x][source_y].addTransition( room, dest_x, dest_y )
		
		
	
